﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using ADODB;
using System.Diagnostics;

namespace SybaseDataResolver
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void btnConnect_Click(object sender, EventArgs e)
        {
            /*string connectionString = "Provider=ASAProv;Password=sql;Persist Security Info=True;User ID=dba";
            using (OleDbConnection conn = new OleDbConnection(connectionString))
            {
                conn.Open();
                OleDbCommand cmd = new OleDbCommand("select * from ISA.table_description where table_sys_type='D'", conn);
                OleDbDataAdapter adapter = new OleDbDataAdapter(cmd);
                DataSet ds = new DataSet();
                adapter.Fill(ds);
                
                this.dataGridView1.DataSource = ds;
                // 数据现在存储在data中，可以进行进一步处理
            }*/
            
        }



        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void btnConnect_Click_1(object sender, EventArgs e)
        {
            Connection conn = AdoDBConnHelper.NewConnection();
            DataTable dt = AdoDBConnHelper.execDataTable(conn, "select table_name,table_auxi_name,table_data_type,'0' 记录行数 from ISA.table_description where table_sys_type='D' union select 'is_files','原文表','','0' ");
            this.dataGridView1.DataSource = dt;

            conn.Close();
        }

        string sybasedir = "F:\\git45\\Sybase\\SQL Anywhere 7\\win32";
        private void btnChooseDB_Click(object sender, EventArgs e)
        {
            this.openFileDialog1.Filter = "sybase数据库文件(.db) | *.db";
            this.openFileDialog1.InitialDirectory = Application.StartupPath;
            if (this.openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                this.txtDBFilePath.Text = this.openFileDialog1.FileName;
            }
            else{
                MessageBox.Show("必须选择DB文件");
                return;
            }

            sybasedir = Application.StartupPath + "\\win32";
            ProcessStartInfo psi = new ProcessStartInfo();
            psi.FileName = sybasedir + "\\dbeng7.exe";
            //加-ga参数会在所有数据库连接关闭时 自动退出进程
            psi.Arguments = " " + this.txtDBFilePath.Text + " -r -quiet";
            Process proc = Process.Start(psi);

            string dboledb7path = sybasedir + "\\dboledb7.dll";
            string dbodbc7path = sybasedir + "\\dbodbc7.dll";
            Process.Start("regsvr32"," \"" + dboledb7path + "\"");
            Process.Start("regsvr32", " \"" + dbodbc7path + "\"");
        }

        private void dataGridView1_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            if(e.RowIndex > -1)
            {
                string tableName = this.dataGridView1.Rows[e.RowIndex].Cells[0].Value.ToString();
                
                FrmShowPertableData frmShowPertableData = new FrmShowPertableData(tableName);
                frmShowPertableData.ShowDialog();
            }
        }

        
    }
}
